package com.test.NIODemo;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * ${DESCRIPTION}
 *
 * @author muxiaoyu
 * @create 2017/06/09 11:25
 **/
public class NIODemo {
    public static void main(String[] args) throws Exception {
        RandomAccessFile afile = new RandomAccessFile("D:/998.txt", "rw");
        FileChannel channel = afile.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(10);
        int read = channel.read(buf);

        while (read != -1) {
            System.out.println("READ:" + read);
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.print((char) buf.get());
            }
            buf.clear();
            read = channel.read(buf);
        }
        afile.close();
    }

    @Test
    public void test777() throws UnsupportedEncodingException {
        String original = "这是一段中文";
        byte[] gbks = original.getBytes("GBK");
        byte[] u8 = original.getBytes("UTF-8");
        System.out.println(new String(gbks, "UTF-8"));
        System.out.println(new String(u8, "GBK"));
    }

    @Test
    public void testBuffer() throws IOException {
        RandomAccessFile file = new RandomAccessFile("d:\\String.txt", "rw");
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        FileChannel channel = file.getChannel();
        int read = channel.read(byteBuffer);
        char[] chars = new char[1000];
        while (read != -1) {
            System.out.println("READ:" + read);
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()) {
                for (int i = 0; i < byteBuffer.limit(); i++) {
                    chars[i] = (char) byteBuffer.get();
                }
            }
            byteBuffer.clear();
            read = channel.read(byteBuffer);
        }
        System.out.println(new String(chars));
    }

    @Test
    public void channelTest() {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("d:\\String.txt")));
            Object o = objectInputStream.readObject();
            System.out.println(o.getClass());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void test88() throws Exception {
        FileInputStream fileInputStream = new FileInputStream(new File("d:\\String.txt"));
        FileChannel channel = fileInputStream.getChannel();
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int read = channel.read(allocate);
        char[] chars = new char[1000];
        while (read != -1) {
            System.out.println("READ:" + read);
            allocate.flip();
            while (allocate.hasRemaining()) {
                for (int i = 0; i < allocate.limit(); i++) {
                    chars[i] = (char) allocate.get();
                }
            }
            allocate.clear();
            read = channel.read(allocate);
        }
        System.out.println(new String(chars));
    }
}
